<template>
  <div style="width:100%;height:100%">
    <div class="maintenance-content-handle">
      <el-button type="primary"
                 @click="openDialog">新增</el-button>
      <!-- <el-button @click="resetSearch">导出</el-button> -->
    </div>
    <el-divider border-style="dashed" />
    <el-table :data="prizesLevelsListData"
              border
              stripe
              style="width:100%;height:100%;">
      <el-table-column fixed
                       prop="lid"
                       label="序号"></el-table-column>
      <el-table-column prop="levelName"
                       label="等级"></el-table-column>
      <el-table-column prop="phValue"
                       label="PH值">
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="primary"
                     @click="editItem(scope.row)">编辑</el-button>
          <el-button type="danger"
                     @click="deleteItem(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>

  <el-dialog v-model="levelsVisible"
             :title="stateVal == 'add'? '新增奖品等级' : '编辑奖品等级'"
             width="500"
             draggable
             overflow>
    <div class="maintenance-form">
      <el-form :model="formData"
               :rules="rules"
               ref="levelsForm">
        <el-row :gutter="10">
          <el-col :span="24">
            <el-form-item label="序号"
                          prop="lid">
              <el-input v-model="formData.lid"
                        :disabled="stateVal == 'add'? false : true" />
            </el-form-item>
          </el-col>

        </el-row>

        <el-row :gutter="10">
          <el-col :span="24">
            <el-form-item label="等级"
                          prop="levelName">
              <el-input v-model="formData.levelName" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="24">
            <el-form-item label="PH值"
                          prop="phValue">
              <el-input v-model="formData.phValue" />
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 提交按钮 -->
        <el-form-item>
          <div style="width: 100%;text-align: center;">
            <el-button type="primary"
                       @click="submitForm">{{stateVal == 'add'? '提交' : '编辑'}}</el-button>
            <el-button @click="resetForm"
                       v-if="stateVal == 'add'">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </el-dialog>
</template>

<script lang='ts' setup>
import {
  ref,
  reactive,
  computed,
  watchEffect,
  watch,
  onBeforeMount,
  onMounted,
  onBeforeUpdate,
  onUpdated,
  onBeforeUnmount,
  onUnmounted,
} from 'vue'

/* 接口 */
import apiHttpFn from '@/api/index'
import { log } from 'console'
import { fa } from 'element-plus/es/locales.mjs'

interface PrizesLevelsListData {
  lid: number | null
  levelName: string
  phValue: number | null
}

// 定义数据

const tokenData = localStorage.getItem('GIS_USER_LOGIN_IDSTR') || ''

// 表单验证规则
const rules = {
  lid: [{ required: true, message: '请输入序号', trigger: 'change' }],
  levelName: [{ required: true, message: '请输入等级名称', trigger: 'change' }],
  phValue: [{ required: true, message: '请输入PH值', trigger: 'change' }],
}

// 奖品等级数据
const prizesLevelsListData = ref<PrizesLevelsListData[]>()

// 等级弹窗是否显示
const levelsVisible = ref(false)

// 表单新增
const levelsForm = ref()

// 当前状态
const stateVal = ref('add')

// 表单数据
const formData = ref<PrizesLevelsListData>({
  lid: null,
  levelName: '',
  phValue: null,
})

const emits = defineEmits(['submitFormAdmin'])

const openDialog = () => {
  resetForm()
  stateVal.value = 'add'
  levelsVisible.value = true
}

const editItem = (row) => {
  console.log(row)
  stateVal.value = 'edit'
  formData.value = row
  levelsVisible.value = true
}

const getLevesData = () => {
  apiHttpFn.getPrizesLevelsListHttp(tokenData).then((res) => {
    prizesLevelsListData.value = res.data?.data || []
    console.log(res)
  })
}

const submitForm = () => {
  if (stateVal.value === 'edit') {
    apiHttpFn.updatePrizesLevelHttp(tokenData, formData.value).then((res) => {
      console.log(res)
      getLevesData()
      levelsVisible.value = false
      emits('submitFormAdmin', res)
    })
  } else {
    levelsForm.value.validate((valid) => {
      if (valid) {
        // 提交表单逻辑
        console.log('提交数据:', formData.value)
        apiHttpFn.addPrizesLevelHttp(tokenData, formData.value).then((res) => {
          console.log(res)
          getLevesData()
          levelsVisible.value = false
          emits('submitFormAdmin', res)
        })
      }
    })
  }
}
const resetForm = () => {
  levelsForm.value?.resetFields()
}

const deleteItem = (row) => {
  apiHttpFn.delPrizesLevelHttp(tokenData, { lid: row.lid }).then((res) => {
    console.log(res)
    getLevesData()
  })
}

onBeforeMount(() => {
  getLevesData()
})
onMounted(() => {
  console.log('onMounted')
})
onUnmounted(() => {
  console.log('onUnmounted')
})

defineExpose({
  getLevesData,
})
</script>
<style lang='scss' scoped>
</style>
